package util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class JedisTool {
    @Autowired
    private RedisTemplate redisTemplate;

    public void set4Value(String key, String value){
        set4Value(key,value, Dictionary.REDIS.DEFAULT_TIME);
    }
    public void set4Value(String key, String value,Long timeout){
        ValueOperations<String,String> vo = redisTemplate.opsForValue();
        vo.set(key,value,timeout,TimeUnit.SECONDS);
    }
    public void deletKey(String key){
        redisTemplate.delete(key);
    }
    public String getValue(String key){
        ValueOperations<String,String> vo = redisTemplate.opsForValue();
        return vo.get(key);
    }
    public void flushTime(String key,Long time){
        ValueOperations<String,String> vo = redisTemplate.opsForValue();
        String value = vo.get(key);
        vo.set(key,value,time,TimeUnit.SECONDS);
    }
    public Long getTTL(String key){
//        ValueOperations<String,String> vo = redisTemplate.opsForValue();
        return redisTemplate.getExpire(key);
    }

}
